#ifndef OUTPUTPREVIEWWIDGET_H
#define OUTPUTPREVIEWWIDGET_H

#include <QWidget>
#include <QPaintEvent>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QComboBox>
#include <QCheckBox>
#include "outputsettings.h"

class PreviewPageWidget;

class OutputPreviewWidget : public QWidget
{
    Q_OBJECT

public:
    explicit OutputPreviewWidget(QWidget *parent = nullptr);
    
    void setSettings(const OutputSettings& settings);
    void setSamplePages(const QList<QImage>& pages);
    void setCurrentFileFirstPage(const QImage& firstPage);

public slots:
    void showCoverPreview();
    void showContentPreview();

private:
    void setupUI();
    void updatePreview();
    
    QVBoxLayout* m_layout;
    QHBoxLayout* m_controlLayout;
    QPushButton* m_showCoverButton;
    QPushButton* m_showContentButton;
    QLabel* m_zoomLabel;
    QComboBox* m_zoomCombo;
    QCheckBox* m_enableMarginsCheck;
    QScrollArea* m_scrollArea;
    PreviewPageWidget* m_previewPage;
    
    OutputSettings m_settings;
    QList<QImage> m_samplePages;
    QImage m_currentFileFirstPage;
    bool m_showingCover;
    double m_zoomFactor;
    bool m_enableMargins;
};

// 实际绘制预览页面的widget
class PreviewPageWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PreviewPageWidget(QWidget *parent = nullptr);
    
    void setSettings(const OutputSettings& settings);
    void setSampleImage(const QImage& image);
    void setShowCover(bool showCover);
    void setZoomFactor(double zoom);
    void setMarginsEnabled(bool enabled);
    void setCurrentFileFirstPage(const QImage& firstPage);

protected:
    void paintEvent(QPaintEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    QSize sizeHint() const override;
    bool hasHeightForWidth() const override { return true; }
    int heightForWidth(int w) const override;

private:
    void drawCoverPage(QPainter& painter, const QRect& pageRect);
    void drawContentPage(QPainter& painter, const QRect& pageRect);
    void drawHeaderFooter(QPainter& painter, const QRect& pageRect);
    QRect getContentRect(const QRect& pageRect) const;
    
    // 真实A4画布渲染
    QImage createRealA4Canvas() const;
    void renderToRealA4Canvas(QImage& canvas, bool isCover) const;
    QSize calculateRealA4Size() const;
    void renderCoverPageToCanvas(QPainter& painter, const QRect& canvasRect) const;
    void renderContentPageToCanvas(QPainter& painter, const QRect& canvasRect) const;
    void renderHeaderFooterToCanvas(QPainter& painter, const QRect& canvasRect) const;
    // 旧对比引导已移除，逻辑整合到 paintEvent 流程
    
    OutputSettings m_settings;
    QImage m_sampleImage;
    QImage m_currentFileFirstPage;
    bool m_showCover;
    double m_zoomFactor;
    bool m_marginsEnabled;
    
    // A4页面尺寸 (毫米)
    static constexpr double A4_WIDTH_MM = 210.0;
    static constexpr double A4_HEIGHT_MM = 297.0;
    
    // 转换函数
    double mmToPixels(double mm) const;
    QRect mmToPixels(double x, double y, double w, double h) const;
    
    // 动态尺寸计算
    QSize calculateOptimalPageSize(const QSize& availableSize) const;
    
    // 拖动相关
    bool m_dragging;
    QPoint m_dragStartPos;
    QPoint m_dragOffset;
    int m_draggedElement; // 0=title, 1=subtitle, 2=author, 3=date
    
    // 封面页元素位置（相对于页面中心的偏移，单位：像素）
    QPoint m_titleOffset;
    QPoint m_subtitleOffset;
    QPoint m_authorOffset;
    QPoint m_dateOffset;
};

#endif // OUTPUTPREVIEWWIDGET_H
